package com.base.cn.platform.os.service.op.affiche.tmp;

import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.op.affiche.OpReceiveAffiche;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.CuserProfile;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 直播提前30分钟提醒
 *
 * @author s.li
 * @date 2019/05/29
 */
@Component
public class LiveSendTemAffice extends SendTemAffice {

    /**
     * 初始化直播发送消息
     * @param arr
     * @param tmpEnum 模板类型
     * @param userList 用户列表
     * @param dataListMap 业务数据 {userId:直接课程数据列表}，直接课程数据列表=[{"liveTime":"直播时间","courseData":"[套餐名-]直播课程名-目录名"}]
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> initMsg(Object[] arr, WXTmpEnum tmpEnum, List<CusUser> userList, Map<BigDecimal,List> dataListMap) {
        initService(arr);

        String liveVarNum1 = smsTempConfigMap.get("liveVarNum1");
        String liveVarNum2 = smsTempConfigMap.get("liveVarNum2");
        String liveRemark = smsTempConfigMap.get("liveRemark");
        String liveMsgType = smsTempConfigMap.get("liveMsgType");

        if(StringUtils.isNotEmpty(liveVarNum1)
                && StringUtils.isNotEmpty(liveVarNum2)
                && StringUtils.isNotEmpty(liveMsgType)){
            Map<String,Object> msg = new HashMap<>();
            boolean status = false;
            if(liveMsgType.contains(MsgType.SITE.getKey())){
                List<OpReceiveAffiche> receiveAfficheList = new ArrayList<>();
                userList.forEach(user -> {
                    List liveDataList = dataListMap.get(user.getId());
                    liveDataList.forEach(liveData->{
                        OpReceiveAffiche affiche = new OpReceiveAffiche();
                        affiche.setMsgType(1);
                        affiche.setStatus(1);
                        affiche.setUserId(user.getId());
                        StringBuilder orderContext = new StringBuilder();
                        Map<String,String> liveDataMap = (Map<String,String>)liveData;
                        orderContext.append(liveDataMap.get("courseData")+"直播即将开始，");
                        orderContext.append(liveVarNum1.replace("{1}",user.getNickname()));
                        orderContext.append("  ");
                        orderContext.append(liveVarNum2.replace("{2}", liveDataMap.get("liveTime")));
                        if(StringUtils.isNotEmpty(liveRemark)){
                            orderContext.append("  ");
                            orderContext.append(liveRemark);
                        }
                        affiche.setContext(orderContext.toString());
                        receiveAfficheList.add(affiche);
                    });
                });
                msg.put(SITE_MESSAGE_KEY,receiveAfficheList);
                status = true;
            }
            if(liveMsgType.contains(MsgType.WX.getKey())){
                List<CuserProfile> cuserProfileList =this.getCuserProfileList(userList);
                if(ObjectUtils.isNotEmpty(cuserProfileList)){
                    Map<BigDecimal,CusUser> userMap = userList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
                    String tmpId = getTmpId(tmpEnum);
                    List<JSONObject> jsonObjectList = new ArrayList<>();
                    cuserProfileList.forEach(cuserProfile -> {
                        List liveDataList = dataListMap.get(cuserProfile.getUserId());
                        CusUser cusUser = userMap.get(cuserProfile.getUserId());
                        liveDataList.forEach(liveData->{
                            Map<String,String> liveDataMap = (Map<String,String>)liveData;
                            //设置参数
                            JSONObject obj = new JSONObject();
                            obj.put("touser", cuserProfile.getOpenId());
                            obj.put("template_id", tmpId);
                            //微信消息数据内容
                            Map<String,Map<String,String>> data = new HashMap<>();
                            //消息标题
                            Map<String,String> first = new HashMap<>();
                            first.put("value","["+liveDataMap.get("courseData")+"]直播课准备开始提醒");
                            first.put("color","#173177");
                            data.put("first",first);

                            Map<String,String> keyword1 = new HashMap<>();
                            keyword1.put("value",cusUser.getNickname());
                            keyword1.put("color","#173177");
                            data.put("keyword1",keyword1);

                            Map<String,String> keyword2 = new HashMap<>();
                            keyword2.put("value", liveDataMap.get("liveTime"));
                            keyword2.put("color","#173177");
                            data.put("keyword2",keyword2);
                            this.setRemarkMap(data,liveRemark);
                            obj.put("data", data);
                            jsonObjectList.add(obj);
                        });
                    });
                    msg.put(WX_MESSAGE_KEY,jsonObjectList);
                    status = true;
                }
            }
            if(status){
                msg.put(MSG_TYPE,liveMsgType);
                return msg;
            }
        }
        return null;
    }
}
